package com.mobi.cacti.ns.view;

import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.AlertDialog;
import org.holoeverywhere.widget.Button;
import org.holoeverywhere.widget.EditText;
import org.holoeverywhere.widget.ProgressBar;

import com.mobi.cacti.R;
import com.mobi.cacti.rabbit.controllers.LoginActivityController;
import com.mobi.cacti.rabbit.controllers.LoginActivityControllerCallback;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.ReqMobileLogin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.view.View.OnClickListener;

public class LoginActivity extends Activity implements
		LoginActivityControllerCallback {

	private LoginActivityController loginActivityController;

	private EditText usernameEditText;
	private EditText passwordEditText;
	private Button submitButton;

	// private TextView errorMessage;
	private ProgressBar loginProgressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);

		loginActivityController = LoginActivityController.getInstance();
		loginActivityController.delegate = this;

		ActionBar actionbar = getSupportActionBar();
		actionbar.hide();

		usernameEditText = (EditText) findViewById(R.id.username);
		passwordEditText = (EditText) findViewById(R.id.password);
		submitButton = (Button) findViewById(R.id.submitbutton);

		// errorMessage = (TextView) findViewById(R.id.errormessage);
		loginProgressBar = (ProgressBar) findViewById(R.id.loginprogressbar);

//		 usernameEditText.setText("KBU");//("bu");
//		 passwordEditText.setText("3JNk9tRZ");//("bugraph");

		submitButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				usernameEditText.setEnabled(false);
				passwordEditText.setEnabled(false);
				submitButton.setEnabled(false);

				// errorMessage.setVisibility(View.GONE);
				// errorMessage.setText("");
				loginProgressBar.setVisibility(View.VISIBLE);

				String username = usernameEditText.getText().toString();
				String password = passwordEditText.getText().toString();

				if (loginActivityController.authenticateWithServer(username,
						password) == false) {
					usernameEditText.setEnabled(true);
					passwordEditText.setEnabled(true);
					submitButton.setEnabled(true);
					loginProgressBar.setVisibility(View.INVISIBLE);
				}

			}
		});

	}

	@Override
	public void loginActivityControllerCompleteAuthenticationToServer() {
		usernameEditText.setEnabled(true);
		passwordEditText.setEnabled(true);
		submitButton.setEnabled(true);

		// errorMessage.setVisibility(View.GONE);
		loginProgressBar.setVisibility(View.GONE);

		ReqMobileLogin reqMobileLogin = Cache.reqMobileLogin;

		if (reqMobileLogin == null || !reqMobileLogin.isLoginSuccess()) {
			// Login fail
			// errorMessage.setVisibility(View.VISIBLE);
			// errorMessage.setText(reqMobileLogin.getCodeResponseMessage());

			// if (reqMobileLogin == null) {
			// showAlertDialog("Parse data failed");
			// } else {
			showAlertDialog(reqMobileLogin.getCodeResponseMessage());
			// }
		} else {
			// Login success
			usernameEditText.setText("");
			passwordEditText.setText("");

			// errorMessage.setVisibility(View.GONE);
			loginProgressBar.setVisibility(View.GONE);

			Intent intent = new Intent(getApplicationContext(),
					MainActivity.class);
			startActivity(intent);
		}
	}

	private void showAlertDialog(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Failed").setMessage(message)
				.setPositiveButton("Retry", null);
		// .setNegativeButton("Cancel", null);
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
	}

}
